﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Gmantis.Controls
{
    internal abstract class StringTypeConverter : TypeConverter
    {
        protected StringTypeConverter()
        {
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }
            if (sourceType != typeof(string))
            {
                return (sourceType == typeof(string));
            }
            return true;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType != typeof(string))
            {
                return (destinationType == typeof(string));
            }
            return true;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return this.ConvertFromString(context, culture, (value == null) ? string.Empty : value.ToString());
        }

        public abstract object ConvertFromString(ITypeDescriptorContext context, CultureInfo culture, string text);
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType != typeof(string))
            {
                return new ArgumentNullException("Only supports string ");
            }
            return this.ConvertToString(context, culture, value);
        }

        public abstract string ConvertToString(ITypeDescriptorContext context, CultureInfo culture, object value);
    }
}
